package com.trainoo.nio.file;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Scanner;

/**
 * <p> add description here </p>
 *
 * @author tao.zhou
 * @version 1.0
 * @date 2020/6/30 15:40
 */
@Slf4j
public class ReadFile {

    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();

        // do something
        String filePath = "C:\\Users\\TJ_tr\\Desktop\\20200630_hotline.csv";
        File file = new File(filePath);
        readFile(file);

        long expendTime = System.currentTimeMillis() - start;
        log.info("success in [{}] milliseconds", expendTime);
        log.info("success in [{}] second", expendTime / 1000);
    }

    private static void readFile(File file) throws IOException {
        try (FileInputStream inputStream = new FileInputStream(file); Scanner sc = new Scanner(inputStream, "UTF-8")) {
            while (sc.hasNextLine()) {
                String line = sc.nextLine();
                log.info(line);
            }
            // note that Scanner suppresses exceptions
            if (sc.ioException() != null) {
                throw sc.ioException();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void readFile2(File file) {
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            // 按行读取，每一行都是一条记录
            while ((line = reader.readLine()) != null) {
                log.info(line);
            }
        } catch (IOException e) {
            log.error("解析文件异常", e);
        }
    }
}
